﻿Public Class SqlTableCntrl
    Dim tt As New ToolTip
    Public Event c_deleteKeyPressed As EventHandler
    Public Event c_SqlSelectsChanged As EventHandler
    Public Event c_selected As EventHandler

    Public allowDelete As Boolean = False

    Public table As sqlTable
    Dim WithEvents cms As New ContextMenuStrip
    Dim WithEvents deleteBut As ToolStripItem


  





    Public Sub New(ByRef table As sqlTable)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.table = table
        Me.allowDelete = allowDelete
        Me.tableName.Text = table.tableName & " (" & table.shortName & ")"
        Me.ContextMenuStrip = cms
        tableName.ContextMenuStrip = cms

        Dim allfield As New SQLfield("*", "All", GetType(String))


        Me.selectList.Items.Add(allfield)
        For Each field As SQLfield In table.fields
            Me.selectList.Items.Add(field)
        Next
        setSelected(Me.table.selected)



        AddHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged
    End Sub

    Public Sub selectSelects()
        RemoveHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged

        'refresh selected fields
        selectList.SelectedItems.Clear()


        For Each s As SQLfield In table.SQLselects
            For i = 0 To selectList.Items.Count - 1
                If selectList.Items(i) Is s Then

                    selectList.SetSelected(i, True) 'SelectedItems.Add(ss)
                End If
            Next


        Next

        AddHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged
    End Sub

    Public Sub selectWheres()
        RemoveHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged
        selectList.SelectedItems.Clear()
        AddHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged
    End Sub

    Private Sub createToolTips(ByVal sender As Object, ByVal e As System.EventArgs) Handles selectList.MouseHover
        'the tool tips!

        Dim Point As Point = PointToClient(Windows.Forms.Cursor.Position) - selectList.Location  '(e.X, e.Y)
        ' MsgBox(Point.X & "   " & Point.Y)

        Dim hoverIndex As Integer = selectList.IndexFromPoint(Point)
        If (hoverIndex >= 0 And hoverIndex < selectList.Items.Count) Then


            tt.SetToolTip(selectList, selectList.Items(hoverIndex).Description)
        End If
    End Sub

    Private Sub SqlTableCntrl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        If e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Back Then
            RaiseEvent c_deleteKeyPressed(Me, New EventArgs)
        End If
    End Sub




    Private Sub selectList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        RemoveHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged


        '   If Me.selectList.SelectedIndex = 0 Then
        'Me.selectList.ClearSelected()
        ' Me.selectList.SelectedIndices.Add(0)

        '        End If

        AddHandler selectList.SelectedIndexChanged, AddressOf selectList_SelectedIndexChanged



        RaiseEvent c_SqlSelectsChanged(Me, New EventArgs)


    End Sub

    Private Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cms.Opening
        If Not allowDelete Then 'if not select page!
            e.Cancel = True
        Else

            cms.Items.Clear()
            deleteBut = cms.Items.Add("Delete")
        End If
    End Sub

    Private Sub deleteBut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles deleteBut.Click
        RaiseEvent c_deleteKeyPressed(Me.table, New EventArgs)
    End Sub



    Private Sub SqlTableCntrl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown, tableName.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then RaiseEvent c_selected(Me.table, New EventArgs)
    End Sub

    Sub setSelected(ByVal value As Boolean)
        If value Then
            Me.BackColor = Color.SteelBlue
            Me.tableName.ForeColor = Color.White
        Else
            Me.BackColor = Color.AliceBlue
            Me.tableName.ForeColor = Color.Black
        End If
    End Sub


End Class
